package orchestration.persistence.sqlite;

import orchestration.persistence.OrchestratorAuthority.AndroidProcess;
import orchestration.persistence.OrchestratorAuthority.AndroidProcessExecution;
import orchestration.persistence.OrchestratorAuthority.AndroidProcessExecutionProperty;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class OrchestratorDatabaseHelper extends SQLiteOpenHelper {

	public static final String PROCESSES_DB="processes";
	//processes
	public static final String TABLE_PROCESS="process";
	public static final String[] PROCESS_FIELDS=
	{AndroidProcess._ID,AndroidProcess.NAME,AndroidProcess.PATH};
	//executions
	public static final String TABLE_EXECUTION="execution";
	public static final String[] EXECUTION_FIELDS=
	{AndroidProcessExecution._ID,AndroidProcessExecution.NAME,
	 AndroidProcessExecution.TOVISIT,AndroidProcessExecution.VISITED,
	 AndroidProcessExecution.EXECUTION_STATUS,AndroidProcessExecution.PROCESS_REF,
	 AndroidProcessExecution.CURRENT,AndroidProcessExecution.KEY};
	//properties
	public static final String TABLE_PROPERTIES="properties";
	public static final String[] PROPERTY_FIELDS=
	{AndroidProcessExecutionProperty._ID,AndroidProcessExecutionProperty.ARTIFACT_REF,
	 AndroidProcessExecutionProperty.EXECUTION_REF,AndroidProcessExecutionProperty.VALUE};
	
	private static final int VERSION=9;
	
	public OrchestratorDatabaseHelper(Context context) {
		super(context, PROCESSES_DB, null, VERSION);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE "+ TABLE_PROCESS + " (" +
				AndroidProcess._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
				AndroidProcess.NAME + " TEXT, " +
				AndroidProcess.PATH + " TEXT); ");
		db.execSQL("CREATE TABLE "+ TABLE_EXECUTION + " (" +
				AndroidProcessExecution._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
				AndroidProcessExecution.NAME + " TEXT, " +
				AndroidProcessExecution.TOVISIT + " TEXT, " +
				AndroidProcessExecution.VISITED + " TEXT, " +
				AndroidProcessExecution.EXECUTION_STATUS + " TEXT, " +
				AndroidProcessExecution.CURRENT+ " TEXT, " +
				AndroidProcessExecution.KEY+" TEXT, " +
				AndroidProcessExecution.PROCESS_REF + " INTEGER); "
				);
		db.execSQL("CREATE TABLE "+ TABLE_PROPERTIES + " (" +
				AndroidProcessExecutionProperty._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
				AndroidProcessExecutionProperty.ARTIFACT_REF + " TEXT, " +
				AndroidProcessExecutionProperty.EXECUTION_REF + " INTEGER, " +
				AndroidProcessExecutionProperty.VALUE + " TEXT); "
				);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		db.execSQL("DROP TABLE IF EXISTS "+ TABLE_PROCESS);
		db.execSQL("DROP TABLE IF EXISTS "+ TABLE_EXECUTION);
		db.execSQL("DROP TABLE IF EXISTS "+ TABLE_PROPERTIES);
		onCreate(db);
	}

}
